# Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

cmake_minimum_required(VERSION 3.0)

project(ohmw)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

option(WITH_DORA "Build with DORA" ON)
option(WITH_ROS "Build with ROS" OFF)
option(WITH_ROS2 "Build with ROS2" OFF)
option(BUILD_TESTS "Build tests" ON)
option(BUILD_EXAMPLES "Build examples" ON)

set(DORA_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../dora" CACHE PATH "Path to ohos DORA repository")

set(RUST_TARGET_PATH "${CMAKE_CURRENT_SOURCE_DIR}/target" CACHE PATH "Path to rust target directory")
if(CMAKE_CROSSCOMPILING)
    if(CMAKE_SYSTEM_PROCESSOR STREQUAL "aarch64")
        set(RUST_TARGET_PATH "${RUST_TARGET_PATH}/aarch64-unknown-linux-ohos")
    else()
        message(FATAL_ERROR "Unsupported architecture: ${CMAKE_SYSTEM_PROCESSOR}")
    endif()
endif()
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
    set(RUST_TARGET_PATH "${RUST_TARGET_PATH}/debug")
else()
    set(RUST_TARGET_PATH "${RUST_TARGET_PATH}/release")
endif()

if(WITH_DORA AND NOT IS_DIRECTORY "${DORA_PATH}")
    message(FATAL_ERROR "${DORA_PATH} is not a valid directory")
endif()

include_directories(${CMAKE_CURRENT_SOURCE_DIR})
add_subdirectory(impl)
add_subdirectory(interfaces/c++)

set(CODEGEN_PATH "${RUST_TARGET_PATH}/ohmw-codegen" CACHE PATH "Path to OHMW codegen")

if(BUILD_TESTS)
    add_custom_command(
        OUTPUT ${CMAKE_BINARY_DIR}/generated/msg.stamp
        COMMAND ${CODEGEN_PATH}
        --msgs-dir ${CMAKE_SOURCE_DIR}/idl/msg
        --out-dir ${CMAKE_BINARY_DIR}/generated
        --language cxx
        COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_BINARY_DIR}/generated/msg.stamp
        DEPENDS ${CODEGEN_PATH}
        COMMENT "Generating C++ code from message definitions"
    )

    add_custom_command(
        OUTPUT ${CMAKE_BINARY_DIR}/generated/framework.stamp
        COMMAND ${CMAKE_COMMAND} -E create_symlink
            ${CMAKE_SOURCE_DIR}/framework/c++
            ${CMAKE_BINARY_DIR}/generated/ohmw_framework
        COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_BINARY_DIR}/generated/framework.stamp
        COMMENT "Symlinking framework headers"
    )

    add_custom_target(c++_msg_codegen ALL DEPENDS ${CMAKE_BINARY_DIR}/generated/msg.stamp)
    add_custom_target(c++_framework_symlink ALL DEPENDS ${CMAKE_BINARY_DIR}/generated/framework.stamp)
    include_directories(${CMAKE_BINARY_DIR}/generated)

    enable_testing()
    add_subdirectory(framework/c++/tests)
    add_subdirectory(tests/bincode/c++)
endif()

if(BUILD_EXAMPLES)
    add_subdirectory(examples/c++)
endif()
